#include "UI_Logic_Interface.h"
#include "UI_Logic_API.h"

const bool API_DEBUG = true;

void init() {
    freopen("logic_err_log.txt","w",stderr);
    if(API_DEBUG)cerr<< "init begin"<<endl;
    UI_Logic_Interface::getInstance()->init();
    if(API_DEBUG)cerr<< "init end"<<endl;
}

void loadAI(const char *fileName, int playerId) {
    if(API_DEBUG)cerr<< "loadAI begin"<<endl;
    UI_Logic_Interface::getInstance()->addActMaker(fileName, playerId);
    if(API_DEBUG)cerr<< "loadAI end"<<endl;
}

void run() {
    if(API_DEBUG)cerr<< "run begin"<<endl;
    UI_Logic_Interface::getInstance()->run();
    if(API_DEBUG)cerr<< "run end"<<endl;
}

void sendCommand(const char *command) {
    if(API_DEBUG)cerr<< "sendCommand begin"<<endl;
    Commands pCommand = Commands::from_string(command);
    UI_Logic_Interface::getInstance()->sendCommand(pCommand);
    if(API_DEBUG)cerr<< "sendCommand end"<<endl;
}

void getReplay(char *replay) {
    if(API_DEBUG)cerr<< "getReplay begin"<<endl;
    std::string replayString = UI_Logic_Interface::getInstance()->getReplay();
    strcpy(replay, replayString.c_str());
    if(API_DEBUG)cerr<< "getReplay end"<<endl;
}

void pushInfo(const char* command, int playerId) {
    if(API_DEBUG)cerr<< "pushInfo begin"<<endl;
    UI_Logic_Interface::getInstance()->pushInfo(command, playerId);
    if(API_DEBUG)cerr<< "pushInfo end"<<endl;
}

void getReplayOneturn(char *replay) {
    if(API_DEBUG)cerr<< "getReplayOneturn begin"<<endl;
    std::string replayString = UI_Logic_Interface::getInstance()->getReplayOneturn();
    cerr << replayString << endl;
    strcpy(replay, replayString.c_str());
    if(API_DEBUG)cerr<< "getReplayOneturn end"<<endl;
}

int checkWin() {
    if(API_DEBUG)cerr<< "checkWin begin"<<endl;
    int re = UI_Logic_Interface::getInstance()->checkWin();
    if(API_DEBUG)cerr<< "checkWin end"<<endl;
    return re;
}

void invokeAI() {
    if(API_DEBUG)cerr << "invokeAI begin" << endl;
    UI_Logic_Interface::getInstance()->invokeAI();
    if(API_DEBUG)cerr << "invokeAI end" << endl;
}

void getPlayerCommand(char* result, int playerId){
    if(API_DEBUG)cerr << "getPlayerCommand begin" << endl;
    Commands pCommand = UI_Logic_Interface::getInstance()->getPlayerCommand(playerId);
    if(API_DEBUG)cerr << pCommand.to_string()<<endl<<"size:" + pCommand.to_string().size() << endl;
    strcpy(result, pCommand.to_string().c_str());
    if(API_DEBUG)cerr << "getPlayerCommand end" << endl;
}

void loadFeedback(const char *fileName, int playerId) {
    if(API_DEBUG)cerr << "loadFeedback begin" << endl;
    UI_Logic_Interface::getInstance()->addFeedbackActMaker(fileName, playerId);
    if(API_DEBUG)cerr << "loadFeedback end" << endl;
}

void pushInfoFeedback(const char *command, int playerId) {
    if(API_DEBUG)cerr << "pushInfoFeedback begin" << endl;
    UI_Logic_Interface::getInstance()->pushInfoFeedback(command, playerId);
    if(API_DEBUG)cerr << "pushInfoFeedback end" << endl;
}

void invokeFeedback() {
    if(API_DEBUG)cerr << "invokeFeedback begin" << endl;
    UI_Logic_Interface::getInstance()->invokeFeedback();
    if(API_DEBUG)cerr << "invokeFeedback end" << endl;
}

void getShowInfo(char *result) {
    std::string info = UI_Logic_Interface::getInstance()->fetchShowinfo();
    strcpy(result, info.c_str());
}

int getFinalWinner() {
    return UI_Logic_Interface::getInstance()->checkWinnerFinal();
}

